package sel.me.websocket.learning.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;
import sel.me.java.learning.common.base.utils.security.MD5Utils;
import sel.me.websocket.learning.auth.VeeefunToken;
import sel.me.websocket.learning.domain.AdminUserDO;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {

    
	//用户登录
    @RequestMapping("/bbs/login")
    public RedirectView authorizing(HttpServletRequest request, String userName, String password, String rememberMe) {
//    	Result<Boolean> result= new Result<>();
    	Subject subject = SecurityUtils.getSubject();
        AdminUserDO adminUserDO = new AdminUserDO();
        adminUserDO.setUsername(userName);
        adminUserDO.setPassword(MD5Utils.MD5("123"));
        VeeefunToken token= new VeeefunToken(adminUserDO.getUsername()+"", password, VeeefunToken.selef);
        if ( adminUserDO.getPassword() != null && adminUserDO.getPassword().equals(MD5Utils.MD5(password))){
//        	result.setResult(true);
            if (rememberMe!=null&&!rememberMe.isEmpty()){
                token.setRememberMe(true);
            }
        } else {
            token.setRememberMe(false);
//            result.setResult(false);
        }
        try{
            subject.login(token);//会跳到我们自定义的realm中
        }catch(Exception e){
            e.printStackTrace();
        }
        
        RedirectView mv= new RedirectView("/websocket/page/index");
        return mv;
    }
    
	@RequestMapping(value = "/logout")
    public String logout(){
		Subject subject = SecurityUtils.getSubject();
   	 	subject.logout();
   	 	return "redirect:/";
    }
}
